<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>python内置函数 | 如是的学习博客</title><meta name="keywords" content="python"><meta name="author" content="rushiy"><meta name="copyright" content="rushiy"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="本文分类及部分代码参考https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_44751294&#x2F;article&#x2F;details&#x2F;126217872  内置函数官网文档地址：https:&#x2F;&#x2F;docs.python.org&#x2F;zh-cn&#x2F;3.8&#x2F;library&#x2F;functions.html python内置函数是Python提供的, 可以直接拿来直接用的函数。Python解释器内置了很多函数和类型，">
<meta property="og:type" content="article">
<meta property="og:title" content="python内置函数">
<meta property="og:url" content="https://rushiy.gitee.io/python/python%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/index.html">
<meta property="og:site_name" content="如是的学习博客">
<meta property="og:description" content="本文分类及部分代码参考https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_44751294&#x2F;article&#x2F;details&#x2F;126217872  内置函数官网文档地址：https:&#x2F;&#x2F;docs.python.org&#x2F;zh-cn&#x2F;3.8&#x2F;library&#x2F;functions.html python内置函数是Python提供的, 可以直接拿来直接用的函数。Python解释器内置了很多函数和类型，">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-10-25T08:32:09.000Z">
<meta property="article:author" content="rushiy">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary"><link rel="shortcut icon" href="/img/favicon.jpg"><link rel="canonical" href="https://rushiy.gitee.io/python/python%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'python内置函数',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-19 01:18:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">如是的学习博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">python内置函数</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-10-25T08:32:09.000Z" title="发表于 2022-10-25 16:32:09">2022-10-25</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-18T17:18:40.619Z" title="更新于 2022-11-19 01:18:40">2022-11-19</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="python内置函数"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>本文分类及部分代码参考<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44751294/article/details/126217872">https://blog.csdn.net/weixin_44751294/article/details/126217872</a></p>
</blockquote>
<p>内置函数官网文档地址：<a target="_blank" rel="noopener" href="https://docs.python.org/zh-cn/3.8/library/functions.html">https://docs.python.org/zh-cn/3.8/library/functions.html</a></p>
<p>python内置函数是Python提供的, 可以直接拿来直接用的函数。Python解释器内置了很多函数和类型，可以在任何时候使用它们。常用69个函数如下：</p>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th>python内置函数</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td><code>abs()</code></td>
<td><code>delattr()</code></td>
<td><code>hash()</code></td>
<td><code>memoryview()</code></td>
<td><code>set()</code></td>
</tr>
<tr>
<td><code>all()</code></td>
<td><code>dict()</code></td>
<td><code>help()</code></td>
<td><code>min()</code></td>
<td><code>setattr()</code></td>
</tr>
<tr>
<td><code>any()</code></td>
<td><code>dir()</code></td>
<td><code>hex()</code></td>
<td><code>next()</code></td>
<td><code>slice()</code></td>
</tr>
<tr>
<td><code>ascii()</code></td>
<td><code>divmod()</code></td>
<td><code>id()</code></td>
<td><code>object()</code></td>
<td><code>sorted()</code></td>
</tr>
<tr>
<td><code>bin()</code></td>
<td><code>enumerate()</code></td>
<td><code>input()</code></td>
<td><code>oct()</code></td>
<td><code>staticmethod()</code></td>
</tr>
<tr>
<td><code>bool()</code></td>
<td><code>eval()</code></td>
<td><code>int()</code></td>
<td><code>open()</code></td>
<td><code>str()</code></td>
</tr>
<tr>
<td><code>breakpoint()</code></td>
<td><code>exec()</code></td>
<td><code>isinstance()</code></td>
<td><code>ord()</code></td>
<td><code>sum()</code></td>
</tr>
<tr>
<td><code>bytearray()</code></td>
<td><code>filter()</code></td>
<td><code>issubclass()</code></td>
<td><code>pow()</code></td>
<td><code>super()</code></td>
</tr>
<tr>
<td><code>bytes()</code></td>
<td><code>float()</code></td>
<td><code>iter()</code></td>
<td><code>print()</code></td>
<td><code>tuple()</code></td>
</tr>
<tr>
<td><code>callable()</code></td>
<td><code>format()</code></td>
<td><code>len()</code></td>
<td><code>property()</code></td>
<td><code>type()</code></td>
</tr>
<tr>
<td><code>chr()</code></td>
<td><code>frozenset()</code></td>
<td><code>list()</code></td>
<td><code>range()</code></td>
<td><code>vars()</code></td>
</tr>
<tr>
<td><code>classmethod()</code></td>
<td><code>getattr()</code></td>
<td><code>locals()</code></td>
<td><code>repr()</code></td>
<td><code>zip()</code></td>
</tr>
<tr>
<td><code>compile()</code></td>
<td><code>globals()</code></td>
<td><code>map()</code></td>
<td><code>reversed()</code></td>
<td><code>__import__()</code></td>
</tr>
<tr>
<td><code>complex()</code></td>
<td><code>hasattr()</code></td>
<td><code>max()</code></td>
<td><code>round()</code></td>
<td></td>
</tr>
</tbody></table>
<p>大概可分为如下几类</p>
<ol>
<li>数学运算(7个)</li>
<li>交互操作(2个)</li>
<li>变量操作(2个)</li>
<li>序列操作(8个)</li>
<li>文件操作(1个)</li>
<li>对象操作(7个)</li>
<li>类型转换(24个)</li>
<li>编译执行(4个)</li>
<li>装饰器(3个)</li>
<li>反射操作(8个)</li>
<li>其它（lambda）</li>
</ol>
<h2 id="数字运算类（7个）"><a href="#数字运算类（7个）" class="headerlink" title="数字运算类（7个）"></a>数字运算类（7个）</h2><p>在python中，数字运算类函数有以下15个：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>abs(x)</code></td>
<td>求数值的绝对值</td>
</tr>
<tr>
<td>2</td>
<td><code>divmod(a, b)</code></td>
<td>返回两个数值的商和余数</td>
</tr>
<tr>
<td>3</td>
<td><code>max()</code></td>
<td>返回可迭代对象中的元素中的最大值或者所有参数的最大值</td>
</tr>
<tr>
<td>4</td>
<td><code>min()</code></td>
<td>返回可迭代对象中的元素中的最小值或者所有参数的最小值</td>
</tr>
<tr>
<td>5</td>
<td><code>pow(x, y[, z])</code></td>
<td>返回两个数值的幂运算值或其与指定整数的模值</td>
</tr>
<tr>
<td>6</td>
<td><code>round(x[, n]) </code></td>
<td>对浮点数进行四舍五入求值</td>
</tr>
<tr>
<td>7</td>
<td><code>sum(iterable[, start]) </code></td>
<td>对元素类型是数值的可迭代对象中的每个元素求和</td>
</tr>
</tbody></table>
<h3 id="abs"><a href="#abs" class="headerlink" title="abs()"></a><code>abs()</code></h3><p>用途：求数值的绝对值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">abs</span>(-<span class="number">3.14</span>)</span><br><span class="line"><span class="number">3.14</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">abs</span>(<span class="number">3.14</span>)</span><br><span class="line"><span class="number">3.14</span></span><br></pre></td></tr></table></figure>

<h3 id="divmod-a-b"><a href="#divmod-a-b" class="headerlink" title="divmod(a, b)"></a><code>divmod(a, b)</code></h3><p>用途：返回两个数值的商和余数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">divmod</span>(<span class="number">10</span>, <span class="number">5</span>)</span><br><span class="line">(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">divmod</span>(<span class="number">10</span>, <span class="number">3</span>)</span><br><span class="line">(<span class="number">3</span>, <span class="number">1</span>)</span><br></pre></td></tr></table></figure>

<h3 id="max"><a href="#max" class="headerlink" title="max()"></a><code>max()</code></h3><p>用途：返回可迭代对象中的元素中的最大值或者所有参数的最大值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>(<span class="number">100</span>, <span class="number">200</span>, <span class="number">300</span>)</span><br><span class="line"><span class="number">300</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>(-<span class="number">100</span>, <span class="number">20</span>)</span><br><span class="line"><span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>(<span class="string">&quot;12345&quot;</span>)    <span class="comment"># 传入1个可迭代对象，取其最大元素值</span></span><br><span class="line"><span class="string">&#x27;5&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>])   <span class="comment"># 传入1个可迭代对象，取其最大元素值</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>(-<span class="number">100</span>, <span class="number">20</span>, key = <span class="built_in">abs</span>)  <span class="comment"># 传入了求绝对值函数，则参数都会进行求绝对值后再取较大者</span></span><br><span class="line">-<span class="number">100</span></span><br></pre></td></tr></table></figure>

<h3 id="min"><a href="#min" class="headerlink" title="min()"></a><code>min()</code></h3><p>用途：返回可迭代对象中的元素中的最小值或者所有参数的最小值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">min</span>(<span class="number">100</span>, <span class="number">200</span>, <span class="number">300</span>)</span><br><span class="line"><span class="number">100</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">min</span>(-<span class="number">100</span>, <span class="number">20</span>)</span><br><span class="line">-<span class="number">100</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">min</span>(<span class="string">&quot;12345&quot;</span>)     <span class="comment"># 传入1个可迭代对象，取其最大元素值</span></span><br><span class="line"><span class="string">&#x27;1&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">max</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>])   <span class="comment"># 传入1个可迭代对象，取其最大元素值</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">min</span>(-<span class="number">100</span>, <span class="number">20</span>, key = <span class="built_in">abs</span>)</span><br><span class="line"><span class="number">20</span></span><br></pre></td></tr></table></figure>

<h3 id="pow-x-y-z"><a href="#pow-x-y-z" class="headerlink" title="pow(x, y[, z])"></a><code>pow(x, y[, z])</code></h3><p>用途：返回两个数值的幂运算值或其与指定整数的模值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">pow</span>(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="number">8</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">pow</span>(<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>)     <span class="comment"># 等同于pow(2, 3)的结果对5取余</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">pow</span>(<span class="number">2</span>, <span class="number">3</span>) % <span class="number">5</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">pow</span>(<span class="number">10</span>, <span class="number">2</span>, <span class="number">5</span>)</span><br><span class="line"><span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">10</span> ** <span class="number">2</span> % <span class="number">5</span></span><br><span class="line"><span class="number">0</span></span><br></pre></td></tr></table></figure>

<h3 id="round-x-n"><a href="#round-x-n" class="headerlink" title="round(x[, n])"></a><code>round(x[, n])</code></h3><p>用途：对浮点数进行四舍五入求值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">round</span>(<span class="number">3.49999999</span>)   <span class="comment">## 注意这种数字四舍五入是3，但保留一位及以上小数位的话是3.5</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">round</span>(<span class="number">3.49999999</span>, <span class="number">1</span>)   <span class="comment">## 结果为3.5，在对其四舍五入就是4了</span></span><br><span class="line"><span class="number">3.5</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">round</span>(<span class="built_in">round</span>(<span class="number">3.49999999</span>, <span class="number">2</span>))</span><br><span class="line"><span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">round</span>(<span class="number">3.50000001</span>)</span><br><span class="line"><span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">round</span>(<span class="number">3.50000001</span>, <span class="number">1</span>)</span><br><span class="line"><span class="number">3.5</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">round</span>(<span class="number">3.50000001</span>, <span class="number">4</span>)</span><br><span class="line"><span class="number">3.5</span></span><br></pre></td></tr></table></figure>

<h3 id="sum-iterable-start"><a href="#sum-iterable-start" class="headerlink" title="sum(iterable[, start]) "></a><code>sum(iterable[, start]) </code></h3><p>用途：对元素类型是数值的可迭代对象中的每个元素求和</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sum</span>((<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>))    <span class="comment"># 传入可迭代对象</span></span><br><span class="line"><span class="number">10</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sum</span>((<span class="number">1.5</span>, <span class="number">2.5</span>, <span class="number">3.5</span>, <span class="number">4.5</span>))</span><br><span class="line"><span class="number">12.0</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sum</span>((<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>),-<span class="number">10</span>)</span><br><span class="line"><span class="number">0</span></span><br></pre></td></tr></table></figure>

<h2 id="交互操作-2个"><a href="#交互操作-2个" class="headerlink" title="交互操作(2个)"></a>交互操作(2个)</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>print()</code></td>
<td>向标准输出对象打印输出</td>
</tr>
<tr>
<td>2</td>
<td><code>input()</code></td>
<td>读取用户输入值</td>
</tr>
</tbody></table>
<h3 id="print"><a href="#print" class="headerlink" title="print()"></a><code>print()</code></h3><p>用途：向标准输出对象打印输出</p>
<ul>
<li><code>seq</code>：指定连接符，默认为空格</li>
<li><code>end</code>：指定结尾的分隔符，默认以<code>\n</code>为分隔符</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;orage&quot;</span>, <span class="string">&quot;banana&quot;</span>)</span><br><span class="line">apple orage banana</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;orage&quot;</span>, <span class="string">&quot;banana&quot;</span>, sep=<span class="string">&quot;++&quot;</span>)  <span class="comment"># 指定连接符为++</span></span><br><span class="line">apple++orage++banana</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>L = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> i <span class="keyword">in</span> L:</span><br><span class="line"><span class="meta">... </span>    <span class="built_in">print</span>(i, end=<span class="string">&quot;/&quot;</span>)</span><br><span class="line">...</span><br><span class="line"><span class="number">1</span>/<span class="number">2</span>/<span class="number">3</span>/<span class="number">4</span>/&gt;&gt;&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">## 转义字符：</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&quot;编程语言:\n\tC\n\tJava\n\tPython&quot;</span>)</span><br><span class="line">编程语言:</span><br><span class="line">        C</span><br><span class="line">        Java</span><br><span class="line">        Python</span><br><span class="line">        </span><br><span class="line"><span class="comment">## 使用\或r都可使转义字符失效</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">r&quot;编程语言:\n\tC\n\tJava\n\tPython&quot;</span>)</span><br><span class="line">编程语言:\n\tC\n\tJava\n\tPython</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&quot;编程语言:\\n\\tC\\n\\tJava\\n\\tPython&quot;</span>)</span><br><span class="line">编程语言:\n\tC\n\tJava\n\tPython</span><br></pre></td></tr></table></figure>

<h3 id="input"><a href="#input" class="headerlink" title="input()"></a><code>input()</code></h3><p>用途：读取用户输入值</p>
<p>不要相信用户的输入。用户的输入应该进行一些处理，如去掉前后的空格等。</p>
<blockquote>
<p>input()函数的返回结果是一个字符串，如果获取的是数字的话需要使用int()函数转换</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 获取用户名</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>username = <span class="built_in">input</span>(<span class="string">&#x27;please input your name: &#x27;</span>)</span><br><span class="line">please <span class="built_in">input</span> your name: zhangsan</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>username</span><br><span class="line"><span class="string">&#x27;zhangsan&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 从用户处获取的输入应该进行一些简单的处理</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>username = <span class="built_in">input</span>(<span class="string">&#x27;please input your name: &#x27;</span>)</span><br><span class="line">please <span class="built_in">input</span> your name:   zhangsan   <span class="comment"># 此处在zhangsan前后输出了一些空格</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>username        <span class="comment"># 这里就能看出来在zhangsan前后输入的空格，对于这种就应该对用户的输入进行删除空格处理</span></span><br><span class="line"><span class="string">&#x27;  zhangsan   &#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>username.strip()</span><br><span class="line"><span class="string">&#x27;zhangsan&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 获取用户年龄</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&#x27;please input your age: &#x27;</span>))</span><br><span class="line">please <span class="built_in">input</span> your age: <span class="number">18</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age</span><br><span class="line"><span class="number">18</span></span><br></pre></td></tr></table></figure>

<h2 id="变量操作-2个"><a href="#变量操作-2个" class="headerlink" title="变量操作(2个)"></a>变量操作(2个)</h2><table>
<thead>
<tr>
<th>序号</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>globals()</code></td>
<td>返回当前作用域内的全局变量和其值组成的字典</td>
</tr>
<tr>
<td>2</td>
<td><code>locals</code></td>
<td>返回当前作用域内的局部变量和其值组成的字典</td>
</tr>
</tbody></table>
<h3 id="globals"><a href="#globals" class="headerlink" title="globals()"></a><code>globals()</code></h3><p>用途：返回当前作用域内的全局变量和其值组成的字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">globals</span>()</span><br><span class="line">&#123;<span class="string">&#x27;__name__&#x27;</span>: <span class="string">&#x27;__main__&#x27;</span>, <span class="string">&#x27;__doc__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__package__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__loader__&#x27;</span>: &lt;<span class="keyword">class</span> <span class="string">&#x27;_frozen_importlib.BuiltinImporter&#x27;</span>&gt;, <span class="string">&#x27;__spec__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__annotations__&#x27;</span>: &#123;&#125;, <span class="string">&#x27;__builtins__&#x27;</span>: &lt;module <span class="string">&#x27;builtins&#x27;</span> (built-<span class="keyword">in</span>)&gt;&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>name = <span class="string">&quot;zhangsan&quot;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">globals</span>()</span><br><span class="line">&#123;<span class="string">&#x27;__name__&#x27;</span>: <span class="string">&#x27;__main__&#x27;</span>, <span class="string">&#x27;__doc__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__package__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__loader__&#x27;</span>: &lt;<span class="keyword">class</span> <span class="string">&#x27;_frozen_importlib.BuiltinImporter&#x27;</span>&gt;, <span class="string">&#x27;__spec__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;__annotations__&#x27;</span>: &#123;&#125;, <span class="string">&#x27;__builtins__&#x27;</span>: &lt;module <span class="string">&#x27;builtins&#x27;</span> (built-<span class="keyword">in</span>)&gt;, <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;zhangsan&#x27;</span>&#125;   <span class="comment"># 多了一个&#x27;name&#x27;: &#x27;zhangsan&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="locals"><a href="#locals" class="headerlink" title="locals"></a><code>locals</code></h3><p>用途：返回当前作用域内的局部变量和其值组成的字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">f</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;before define a &#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">locals</span>())            <span class="comment"># 在定义a之前看看作用域内的变量信息</span></span><br><span class="line">    a = <span class="number">1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;after define a&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">locals</span>())            <span class="comment"># 在定义a之后看看作用域内的变量信息</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>f()</span><br><span class="line">before define a</span><br><span class="line">&#123;&#125;                 <span class="comment"># 输出为空，表示在定义a=1前，作用域内无变量</span></span><br><span class="line">after define a</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>&#125;           <span class="comment"># 定义了a=1后，输出了a的值，表示作用域内有一个变量a</span></span><br></pre></td></tr></table></figure>
<h2 id="序列操作-8个"><a href="#序列操作-8个" class="headerlink" title="序列操作(8个)"></a>序列操作(8个)</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>all()</code></td>
<td>判断可迭代对象的每个元素是否都为True值</td>
</tr>
<tr>
<td>2</td>
<td><code>any()</code></td>
<td>判断可迭代对象的元素是否有为True值的元素</td>
</tr>
<tr>
<td>3</td>
<td><code>filter()</code></td>
<td>使用指定方法过滤可迭代对象的元素</td>
</tr>
<tr>
<td>4</td>
<td><code>map()</code></td>
<td>使用指定方法去作用传入的每个可迭代对象的元素，生成新的可迭代对象</td>
</tr>
<tr>
<td>5</td>
<td><code>next()</code></td>
<td>返回可迭代对象中的下一个元素值</td>
</tr>
<tr>
<td>6</td>
<td><code>reversed()</code></td>
<td>反转序列生成新的可迭代对象</td>
</tr>
<tr>
<td>7</td>
<td><code>sorted()</code></td>
<td>对可迭代对象进行排序，返回一个新的列表</td>
</tr>
<tr>
<td>8</td>
<td><code>zip()</code></td>
<td>聚合传入的每个迭代器中相同位置的元素，返回一个新的元组类型迭代器</td>
</tr>
</tbody></table>
<h3 id="all"><a href="#all" class="headerlink" title="all()"></a><code>all()</code></h3><p>用途：判断可迭代对象的每个元素是否都为True值，只要有一个为假，则整个结果为假</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">all</span>([<span class="number">1</span>, <span class="number">2</span>])</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">all</span>([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>])   <span class="comment"># 有一个元素是0，整个结果为假</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">all</span>(()) <span class="comment"># 空元组</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">all</span>(&#123;&#125;) <span class="comment"># 空字典</span></span><br><span class="line"><span class="literal">True</span></span><br></pre></td></tr></table></figure>

<h3 id="any"><a href="#any" class="headerlink" title="any()"></a><code>any()</code></h3><p>用途：判断可迭代对象的元素是否有为True值的元素，只要有一个结果为真，则为真</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">any</span>([<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>])</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">any</span>([<span class="number">0</span>,<span class="number">0</span>])       <span class="comment"># 列表的两个元素都是0，判断为假</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">any</span>([])   <span class="comment"># 空列表</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">any</span>(&#123;&#125;)   <span class="comment"># 空字典</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">any</span>(())   <span class="comment"># 空元组</span></span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure>

<h3 id="filter-function-iterable"><a href="#filter-function-iterable" class="headerlink" title="filter(function, iterable)"></a><code>filter(function, iterable)</code></h3><p>用途：使用指定方法过滤可迭代对象的元素。</p>
<p>请注意， <code>filter(function, iterable)</code> 相当于一个生成器表达式，当<code>function</code>不是 <code>None</code> 的时候为 <code>(item for item in iterable if function(item))</code>；<code>function</code>是 <code>None</code> 的时候为 <code>(item for item in iterable if item)</code> 。</p>
<ul>
<li>案例1：获取给定序列中的奇数</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">list</span>(<span class="built_in">range</span>(<span class="number">1</span>,<span class="number">10</span>)) </span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义奇数判断函数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">def</span> <span class="title function_">if_odd</span>(<span class="params">x</span>): </span><br><span class="line">    <span class="keyword">return</span> x%<span class="number">2</span>==<span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">filter</span>(if_odd,a)) <span class="comment"># 筛选序列中的奇数</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>案例2：<code>filter()</code>经常配合匿名函数<code>lambda()</code>一起使用</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 获取偶数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>even_num = <span class="built_in">list</span>(<span class="built_in">filter</span>(<span class="keyword">lambda</span> x: x % <span class="number">2</span> == <span class="number">0</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>even_num</span><br><span class="line">[<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取奇数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>odd_num = <span class="built_in">list</span>(<span class="built_in">filter</span>(<span class="keyword">lambda</span> x: x % <span class="number">2</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>odd_num</span><br><span class="line">[<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">filter</span>(<span class="keyword">lambda</span> x: x &lt; <span class="string">&#x27;g&#x27;</span>, <span class="string">&#x27;hijack&#x27;</span>)</span><br><span class="line">&lt;<span class="built_in">filter</span> <span class="built_in">object</span> at <span class="number">0x1034b4080</span>&gt;   <span class="comment"># python3</span></span><br></pre></td></tr></table></figure>

<h3 id="map"><a href="#map" class="headerlink" title="map()"></a><code>map()</code></h3><p>用途：使用指定方法去作用传入的每个可迭代对象的元素，生成新的可迭代对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">map</span>(<span class="built_in">ord</span>,<span class="string">&#x27;abcd&#x27;</span>)    <span class="comment"># ord()函数返回对应值的assic码</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">&lt;<span class="built_in">map</span> <span class="built_in">object</span> at <span class="number">0x03994E50</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(a)</span><br><span class="line">[<span class="number">97</span>, <span class="number">98</span>, <span class="number">99</span>, <span class="number">100</span>]</span><br></pre></td></tr></table></figure>

<p>案例2：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">def</span> <span class="title function_">square</span>(<span class="params">x</span>):     <span class="comment"># 定义一个乘方的函数</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">return</span> x * x</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">map</span>(square, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])   <span class="comment"># 对序列中的每个元素执行square函数</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">## 上面的做法可以使用lambda函数，一行代码就能完成</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">map</span>(<span class="keyword">lambda</span> x: x * x, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]) </span><br><span class="line">[<span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">map</span>(<span class="built_in">str</span>, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>])  <span class="comment"># 将序列中的每个元素转换成字符串</span></span><br><span class="line">[<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;4&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">map</span>(<span class="built_in">int</span>, [<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;4&#x27;</span>])  <span class="comment"># 将序列中的每个元素转换成整型</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br></pre></td></tr></table></figure>

<p>案例3：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">double</span>(<span class="params">x</span>):</span><br><span class="line">    <span class="keyword">return</span> <span class="number">2</span> * x</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">triple</span>(<span class="params">x</span>):</span><br><span class="line">    <span class="keyword">return</span> <span class="number">3</span> * x</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">square</span>(<span class="params">x</span>):</span><br><span class="line">    <span class="keyword">return</span> x * x</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>funcs = [double, triple, square]  <span class="comment"># 列表元素是函数对象</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>value = <span class="built_in">list</span>(<span class="built_in">map</span>(<span class="keyword">lambda</span> f: f(<span class="number">4</span>), funcs))   <span class="comment"># 相当于 [double(4), triple(4), square(4)]</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(value)</span><br><span class="line">[<span class="number">8</span>, <span class="number">12</span>, <span class="number">16</span>]</span><br></pre></td></tr></table></figure>

<h3 id="next"><a href="#next" class="headerlink" title="next()"></a><code>next()</code></h3><p>用途：返回可迭代对象中的下一个元素值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">iter</span>(<span class="string">&#x27;我是中国人&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;我&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;是&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;中&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;国&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;人&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)           <span class="comment"># 取完值，再取的话就报错</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">StopIteration</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a, <span class="string">&quot;序列中没有值了&quot;</span>)   <span class="comment"># 传入默认值，待取完序列中的值后，返回默认值，这样就不会报错了</span></span><br><span class="line"><span class="string">&#x27;序列中没有值了&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a, <span class="string">&quot;序列中没有值了&quot;</span>)</span><br><span class="line"><span class="string">&#x27;序列中没有值了&#x27;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>对<code>next()</code>报错的常规处理</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">iter</span>(<span class="string">&#x27;我是中国人&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"><span class="meta">... </span>     <span class="keyword">try</span>:</span><br><span class="line"><span class="meta">... </span>         <span class="built_in">next</span>(a)</span><br><span class="line"><span class="meta">... </span>     <span class="keyword">except</span> StopIteration:</span><br><span class="line"><span class="meta">... </span>         <span class="keyword">break</span></span><br><span class="line">...</span><br><span class="line"><span class="string">&#x27;我&#x27;</span></span><br><span class="line"><span class="string">&#x27;是&#x27;</span></span><br><span class="line"><span class="string">&#x27;中&#x27;</span></span><br><span class="line"><span class="string">&#x27;国&#x27;</span></span><br><span class="line"><span class="string">&#x27;人&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="reversed"><a href="#reversed" class="headerlink" title="reversed()"></a><code>reversed()</code></h3><p>用途：反转序列生成新的可迭代对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">reversed</span>(<span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line">&lt;range_iterator <span class="built_in">object</span> at <span class="number">0x000001A8DFBC7B70</span>&gt;    <span class="comment"># 返回了一个迭代器对象</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(a)</span><br><span class="line">[<span class="number">9</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">0</span>]</span><br></pre></td></tr></table></figure>

<h3 id="sorted"><a href="#sorted" class="headerlink" title="sorted()"></a><code>sorted()</code></h3><p>用途：按照<code>ascii码</code>对可迭代对象进行排序，返回一个新的列表</p>
<blockquote>
<p>注意：未改变原序列</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = [<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;B&#x27;</span>,<span class="string">&#x27;A&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(a)</span><br><span class="line">[<span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sorted</span>(a,key = <span class="built_in">str</span>.lower)   <span class="comment"># 转换成小写后再排序</span></span><br><span class="line">[<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h3 id="zip"><a href="#zip" class="headerlink" title="zip()"></a><code>zip()</code></h3><p>用途：聚合传入的每个迭代器中相同位置的元素，返回一个新的元组类型迭代器</p>
<blockquote>
<p>注意：zip()若长度不同，则取最小长度</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>]   <span class="comment"># 长度为3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]   <span class="comment"># 长度为5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">zip</span>(x,y))        <span class="comment"># 取最小长度3</span></span><br><span class="line">[(<span class="string">&#x27;a&#x27;</span>, <span class="number">4</span>), (<span class="string">&#x27;b&#x27;</span>, <span class="number">5</span>), (<span class="string">&#x27;c&#x27;</span>, <span class="number">6</span>)]</span><br><span class="line"></span><br><span class="line"><span class="comment">## 常用于生成字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>x = [<span class="string">&quot;name&quot;</span>, <span class="string">&quot;age&quot;</span>, <span class="string">&quot;addr&quot;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>y = [<span class="string">&quot;zhangsan&quot;</span>, <span class="number">18</span>, <span class="string">&quot;chengdu&quot;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>D = <span class="built_in">dict</span>(<span class="built_in">zip</span>(x,y))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>D</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;zhangsan&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;addr&#x27;</span>: <span class="string">&#x27;chengdu&#x27;</span>&#125;</span><br></pre></td></tr></table></figure>

<h2 id="文件操作（1个）"><a href="#文件操作（1个）" class="headerlink" title="文件操作（1个）"></a>文件操作（1个）</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>open(filename, mode)</code></td>
<td>使用指定的模式和编码打开文件，返回文件读写对象</td>
</tr>
</tbody></table>
<p>（1）文件读取模式：</p>
<ul>
<li><code>w</code>：打开一个文件只用于写入，如果该文件已存在则打开文件，并且从头开始编辑，即原有内容会被删除。如果该文件不存在，则创建新文件。</li>
<li><code>r</code>：以只读的方式打开文件。文件的指针将会放在文件的开头。</li>
<li><code>b</code>：二进制模式</li>
<li><code>+</code>：打开一个文件进行更新，可读可写</li>
<li><code>a</code>：打开一个文件用于追加，如果该文件已经存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，则创建新文件进行写入。</li>
</ul>
<p>（2）对象方法：</p>
<ul>
<li><code>read(size)</code>：返回整个文件, 如果指定size则返回size个字符；</li>
<li><code>readline()</code>：返回一行；</li>
<li><code>readlines()</code>：返回全部行的一个列表；</li>
<li><code>write()</code>：写入字符；</li>
<li><code>close()</code>：关闭文件，打开文件后一定要记得关闭文件，有始有终；</li>
</ul>
<p>（3）案例</p>
<ul>
<li>普通文件<code>tmp.txt</code></li>
</ul>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">this is 1st line</span><br><span class="line">this is 2nd line</span><br><span class="line">this is 3th line</span><br><span class="line">this is 4th line</span><br><span class="line">this is 5th line</span><br><span class="line">this is 6th line</span><br></pre></td></tr></table></figure>

<ul>
<li>手动打开和关闭文件</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="built_in">open</span>(<span class="string">&#x27;tmp.txt&#x27;</span>, <span class="string">&#x27;rt&#x27;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) </span><br><span class="line">line = a.read()</span><br><span class="line"><span class="built_in">print</span>(line)</span><br><span class="line">a.close()</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;  # 输出结果：</span></span><br><span class="line"><span class="string">this is 1st line</span></span><br><span class="line"><span class="string">this is 2nd line</span></span><br><span class="line"><span class="string">this is 3th line</span></span><br><span class="line"><span class="string">this is 4th line</span></span><br><span class="line"><span class="string">this is 5th line</span></span><br><span class="line"><span class="string">this is 6th line</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>自动关闭文件</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;tmp.txt&#x27;</span>, <span class="string">&#x27;rt&#x27;</span>) <span class="keyword">as</span> f: </span><br><span class="line">    lines = f.readlines() </span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">        <span class="built_in">print</span>(line)</span><br><span class="line">        </span><br><span class="line"><span class="string">&quot;&quot;&quot;  # 输出结果：</span></span><br><span class="line"><span class="string">this is 1st line</span></span><br><span class="line"><span class="string">this is 2nd line</span></span><br><span class="line"><span class="string">this is 3th line</span></span><br><span class="line"><span class="string">this is 4th line</span></span><br><span class="line"><span class="string">this is 5th line</span></span><br><span class="line"><span class="string">this is 6th line</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="对象操作-9个"><a href="#对象操作-9个" class="headerlink" title="对象操作(9个)"></a>对象操作(9个)</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>help()</code></td>
<td>返回对象的帮助信息</td>
</tr>
<tr>
<td>2</td>
<td><code>dir()</code></td>
<td>返回对象或者当前作用域内的属性列表</td>
</tr>
<tr>
<td>3</td>
<td><code>id()</code></td>
<td>返回对象的唯一标识符</td>
</tr>
<tr>
<td>4</td>
<td><code>hash()</code></td>
<td>获取对象的哈希值</td>
</tr>
<tr>
<td>5</td>
<td><code>type()</code></td>
<td>返回对象的类型，或者根据传入的参数创建一个新的类型</td>
</tr>
<tr>
<td>6</td>
<td><code>len()</code></td>
<td>返回对象的长度</td>
</tr>
<tr>
<td>7</td>
<td><code>ascii()</code></td>
<td>返回对象的可打印表字符串表现方式</td>
</tr>
<tr>
<td>8</td>
<td><code>format()</code></td>
<td>格式化显示值</td>
</tr>
<tr>
<td>9</td>
<td><code>vars()</code></td>
<td>返回当前作用域内的局部变量和其值组成的字典，或者返回对象的属性列表</td>
</tr>
</tbody></table>
<h3 id="help"><a href="#help" class="headerlink" title="help()"></a><code>help()</code></h3><p>用途：返回对象的帮助信息</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">help</span>(<span class="built_in">id</span>)  <span class="comment"># 查看id函数的帮助信息</span></span><br><span class="line">Help on built-<span class="keyword">in</span> function <span class="built_in">id</span> <span class="keyword">in</span> module builtins:</span><br><span class="line"></span><br><span class="line"><span class="built_in">id</span>(obj, /)</span><br><span class="line">    Return the identity of an <span class="built_in">object</span>.</span><br><span class="line"></span><br><span class="line">    This <span class="keyword">is</span> guaranteed to be unique among simultaneously existing objects.</span><br><span class="line">    (CPython uses the <span class="built_in">object</span><span class="string">&#x27;s memory address.)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&gt;&gt;&gt; help(str.lower)   # 查看字符串lower方法的帮助信息</span></span><br><span class="line"><span class="string">Help on method_descriptor:</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">lower(self, /)</span></span><br><span class="line"><span class="string">    Return a copy of the string converted to lowercase.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&gt;&gt;&gt; help(str.upper)   # 查看字符串upper方法的帮助信息</span></span><br><span class="line"><span class="string">Help on method_descriptor:</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">upper(self, /)</span></span><br><span class="line"><span class="string">    Return a copy of the string converted to uppercase.</span></span><br></pre></td></tr></table></figure>

<h3 id="dir"><a href="#dir" class="headerlink" title="dir()"></a><code>dir()</code></h3><p>用途：返回对象或者当前作用域内的属性列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> math</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>math                        <span class="comment"># 显示math是一个内置模块</span></span><br><span class="line">&lt;module <span class="string">&#x27;math&#x27;</span> (built-<span class="keyword">in</span>)&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">dir</span>(math)                <span class="comment"># math拥有以下的方法</span></span><br><span class="line">[<span class="string">&#x27;__doc__&#x27;</span>, <span class="string">&#x27;__loader__&#x27;</span>, <span class="string">&#x27;__name__&#x27;</span>, <span class="string">&#x27;__package__&#x27;</span>, <span class="string">&#x27;__spec__&#x27;</span>, <span class="string">&#x27;acos&#x27;</span>, <span class="string">&#x27;acosh&#x27;</span>, <span class="string">&#x27;asin&#x27;</span>, <span class="string">&#x27;asinh&#x27;</span>, <span class="string">&#x27;atan&#x27;</span>, <span class="string">&#x27;atan2&#x27;</span>, <span class="string">&#x27;atanh&#x27;</span>, <span class="string">&#x27;ceil&#x27;</span>, <span class="string">&#x27;comb&#x27;</span>, <span class="string">&#x27;copysign&#x27;</span>, <span class="string">&#x27;cos&#x27;</span>, <span class="string">&#x27;cosh&#x27;</span>, <span class="string">&#x27;degrees&#x27;</span>, <span class="string">&#x27;dist&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;erf&#x27;</span>, <span class="string">&#x27;erfc&#x27;</span>, <span class="string">&#x27;exp&#x27;</span>, <span class="string">&#x27;expm1&#x27;</span>, <span class="string">&#x27;fabs&#x27;</span>, <span class="string">&#x27;factorial&#x27;</span>, <span class="string">&#x27;floor&#x27;</span>, <span class="string">&#x27;fmod&#x27;</span>, <span class="string">&#x27;frexp&#x27;</span>, <span class="string">&#x27;fsum&#x27;</span>, <span class="string">&#x27;gamma&#x27;</span>, <span class="string">&#x27;gcd&#x27;</span>, <span class="string">&#x27;hypot&#x27;</span>, <span class="string">&#x27;inf&#x27;</span>, <span class="string">&#x27;isclose&#x27;</span>, <span class="string">&#x27;isfinite&#x27;</span>, <span class="string">&#x27;isinf&#x27;</span>, <span class="string">&#x27;isnan&#x27;</span>, <span class="string">&#x27;isqrt&#x27;</span>, <span class="string">&#x27;ldexp&#x27;</span>, <span class="string">&#x27;lgamma&#x27;</span>, <span class="string">&#x27;log&#x27;</span>, <span class="string">&#x27;log10&#x27;</span>, <span class="string">&#x27;log1p&#x27;</span>, <span class="string">&#x27;log2&#x27;</span>, <span class="string">&#x27;modf&#x27;</span>, <span class="string">&#x27;nan&#x27;</span>, <span class="string">&#x27;perm&#x27;</span>, <span class="string">&#x27;pi&#x27;</span>, <span class="string">&#x27;pow&#x27;</span>, <span class="string">&#x27;prod&#x27;</span>, <span class="string">&#x27;radians&#x27;</span>, <span class="string">&#x27;remainder&#x27;</span>, <span class="string">&#x27;sin&#x27;</span>, <span class="string">&#x27;sinh&#x27;</span>, <span class="string">&#x27;sqrt&#x27;</span>, <span class="string">&#x27;tan&#x27;</span>, <span class="string">&#x27;tanh&#x27;</span>, <span class="string">&#x27;tau&#x27;</span>, <span class="string">&#x27;trunc&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h3 id="id"><a href="#id" class="headerlink" title="id()"></a><code>id()</code></h3><p>用途：返回对象的唯一标识符，即内存值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>name = <span class="string">&quot;zhangsan&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(name)</span><br><span class="line"><span class="number">2254383256624</span></span><br></pre></td></tr></table></figure>

<h3 id="hash"><a href="#hash" class="headerlink" title="hash()"></a><code>hash()</code></h3><p>用途：获取对象的哈希值，只有不可变对象（如字符串、整型、元组等）才有hash值，可变对象，如列表、字典、集合、没有哈希值。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hash</span>(<span class="string">&quot;zhangsan&quot;</span>)   <span class="comment"># 字符串</span></span><br><span class="line">-<span class="number">8291210672792324925</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hash</span>(<span class="number">1234567</span>)      <span class="comment"># 整型</span></span><br><span class="line"><span class="number">1234567</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hash</span>((<span class="number">1</span>, <span class="string">&quot;zhangsan&quot;</span>))   <span class="comment"># 元组</span></span><br><span class="line"><span class="number">2468767411244581005</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hash</span>([<span class="number">1</span>, <span class="string">&quot;zhangsan&quot;</span>])</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">TypeError: unhashable <span class="built_in">type</span>: <span class="string">&#x27;list&#x27;</span>    <span class="comment"># 提示列表是不可哈希对象</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">## 虽然我们不能对列表进行哈希，但我们可以将列表转换成字符串进行哈希</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hash</span>(<span class="built_in">str</span>([<span class="number">1</span>, <span class="string">&quot;zhangsan&quot;</span>]))</span><br><span class="line">-<span class="number">7837032104649756052</span></span><br></pre></td></tr></table></figure>

<h3 id="type"><a href="#type" class="headerlink" title="type()"></a><code>type()</code></h3><p>用途：返回对象的类型，或者根据传入的参数创建一个新的类型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(<span class="number">1</span>)          <span class="comment"># 整型</span></span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;int&#x27;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(<span class="number">1.2</span>)        <span class="comment"># 浮点型</span></span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;float&#x27;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>([<span class="number">1</span>])        <span class="comment"># 列表</span></span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;list&#x27;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>((<span class="number">1</span>,))       <span class="comment"># 元组</span></span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;tuple&#x27;</span>&gt;</span><br></pre></td></tr></table></figure>

<h3 id="len"><a href="#len" class="headerlink" title="len()"></a><code>len()</code></h3><p>用途：返回对象的长度</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">len</span>(<span class="string">&quot;hello world&quot;</span>)      <span class="comment"># 获取字符串的长度</span></span><br><span class="line"><span class="number">11</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">len</span>([<span class="number">1</span>, <span class="string">&quot;zhangsan&quot;</span>, (<span class="number">1</span>, <span class="string">&quot;abc&quot;</span>)])  <span class="comment"># 获取列表的长度</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">len</span>(<span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"><span class="number">10</span></span><br></pre></td></tr></table></figure>

<h3 id="ascii"><a href="#ascii" class="headerlink" title="ascii()"></a><code>ascii()</code></h3><p>用途：返回对象的可打印表字符串表现方式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## ascii字符</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">ascii</span>(<span class="number">1</span>)</span><br><span class="line"><span class="string">&#x27;1&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">ascii</span>(<span class="string">&#x27;&amp;&#x27;</span>)</span><br><span class="line"><span class="string">&quot;&#x27;&amp;&#x27;&quot;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">ascii</span>(<span class="string">&quot;I love you, china&quot;</span>)</span><br><span class="line"><span class="string">&quot;&#x27;I love you, china&#x27;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 非ascii字符</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">ascii</span>(<span class="string">&#x27;我爱你中国&#x27;</span>)</span><br><span class="line"><span class="string">&quot;&#x27;\\u6211\\u7231\\u4f60\\u4e2d\\u56fd&#x27;&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="format"><a href="#format" class="headerlink" title="format()"></a><code>format()</code></h3><p>用途：格式化显示值</p>
<ul>
<li>进制转换</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 十进制转换成二进制</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10</span>,<span class="string">&#x27;b&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;1010&#x27;</span></span><br><span class="line"><span class="comment"># 十进制转换成二进制，以二进制的方式表示，这种方式一目了然可以看出是二进制，其他进制也适用</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10</span>,<span class="string">&#x27;#b&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;0b1010&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 十进制转换成八进制</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10</span>,<span class="string">&#x27;o&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;12&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 十进制转换成十六进制，小写字母表示</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10</span>,<span class="string">&#x27;x&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;a&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 十进制转换成十六进制，大写字母表示</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10</span>,<span class="string">&#x27;X&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;A&#x27;</span></span><br><span class="line"><span class="comment"># 十进制转换成十六进制，以十六进制的方式表示</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10</span>,<span class="string">&#x27;#X&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;0XA&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 转换unicode成字符</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10</span>,<span class="string">&#x27;c&#x27;</span>) </span><br><span class="line"><span class="string">&#x27;\n&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 二进制转十进制</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">0b1010</span>,<span class="string">&#x27;d&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;10&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 八进制转二进制</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">0o12</span>,<span class="string">&#x27;b&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;1010&#x27;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>快速千分位分割</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">format</span>(<span class="number">10000000000000</span>, <span class="string">&quot;,&quot;</span>)</span><br><span class="line"><span class="string">&#x27;10,000,000,000,000&#x27;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>对齐<ul>
<li><code>^</code>：居中对齐</li>
<li><code>&lt;</code>：左对齐</li>
<li><code>&gt;</code>：右对齐</li>
</ul>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;|&#123;:&lt;10&#125;|&#x27;</span>.<span class="built_in">format</span>(<span class="string">&#x27;python&#x27;</span>))</span><br><span class="line">|python    |</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;|&#123;:□&lt;10&#125;|&#x27;</span>.<span class="built_in">format</span>(<span class="string">&#x27;python&#x27;</span>))</span><br><span class="line">|python□□□□|</span><br></pre></td></tr></table></figure>

<ul>
<li>保留小数位</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;&#123;:.2f&#125;&#x27;</span>.<span class="built_in">format</span>(<span class="number">3.1415926</span>))</span><br><span class="line"><span class="number">3.14</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;&#123;:.2%&#125;&#x27;</span>.<span class="built_in">format</span>(<span class="number">3.1415926</span>))</span><br><span class="line"><span class="number">314.16</span>%</span><br></pre></td></tr></table></figure>

<blockquote>
<ul>
<li><code>:</code>表示要设置的值</li>
<li><code>.2</code>表示保留小数点后两位</li>
<li><code>f</code>表示返回浮点数</li>
<li><code>%</code>表示设置成百分比格式</li>
</ul>
</blockquote>
<ul>
<li>配合<code>print()</code>函数使用</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 顺序传值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;编程语言&#123;&#125;获得&#123;&#125;学分。&#x27;</span>.<span class="built_in">format</span>(<span class="string">&#x27;python&#x27;</span>, <span class="number">100</span>))</span><br><span class="line">编程语言python获得<span class="number">100</span>学分。</span><br><span class="line"></span><br><span class="line"><span class="comment">## 按照指定顺序获取format中的值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;编程语言&#123;1&#125;获得&#123;0&#125;学分。&#x27;</span>.<span class="built_in">format</span>(<span class="number">100</span>, <span class="string">&#x27;python&#x27;</span>))</span><br><span class="line">编程语言python获得<span class="number">100</span>学分。</span><br><span class="line"></span><br><span class="line"><span class="comment">## 使用关键字索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;编程语言&#123;name&#125;获得&#123;score&#125;学分。&#x27;</span>.<span class="built_in">format</span>(name=<span class="string">&#x27;python&#x27;</span>, score=<span class="number">100</span>))</span><br><span class="line">编程语言python获得<span class="number">100</span>学分。</span><br><span class="line"></span><br><span class="line"><span class="comment">## 获取列表值的第一种方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>L = [<span class="string">&#x27;python&#x27;</span>, <span class="number">100</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;编程语言&#123;0&#125;获得&#123;1&#125;学分。&#x27;</span>.<span class="built_in">format</span>(*L))</span><br><span class="line">编程语言python获得<span class="number">100</span>学分。</span><br><span class="line"></span><br><span class="line"><span class="comment">## 获取列表值的第二种方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>L = [<span class="string">&#x27;python&#x27;</span>, <span class="number">100</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;编程语言&#123;0[0]&#125;获得&#123;0[1]&#125;学分。&#x27;</span>.<span class="built_in">format</span>(L))</span><br><span class="line">编程语言python获得<span class="number">100</span>学分。</span><br><span class="line"></span><br><span class="line"><span class="comment">## 获取字典值的第一种方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>D = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&#x27;python&#x27;</span>, <span class="string">&quot;score&quot;</span>: <span class="number">100</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;编程语言&#123;0[name]&#125;获得&#123;0[score]&#125;学分。&#x27;</span>.<span class="built_in">format</span>(D))</span><br><span class="line">编程语言python获得<span class="number">100</span>学分。</span><br><span class="line"></span><br><span class="line"><span class="comment">## 获取字典值的第二种方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>D = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&#x27;python&#x27;</span>, <span class="string">&quot;score&quot;</span>: <span class="number">100</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">print</span>(<span class="string">&#x27;编程语言&#123;name&#125;获得&#123;score&#125;学分。&#x27;</span>.<span class="built_in">format</span>(**D))</span><br><span class="line">编程语言python获得<span class="number">100</span>学分。</span><br></pre></td></tr></table></figure>

<h3 id="vars"><a href="#vars" class="headerlink" title="vars()"></a><code>vars()</code></h3><p>用途：返回当前作用域内的局部变量和其值组成的字典，或者返回对象的属性列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 作用于类实例</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">my_obj</span>(<span class="title class_ inherited__">object</span>):</span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 定义属性之前</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_obj.__dict__</span><br><span class="line">mappingproxy(&#123;<span class="string">&#x27;__module__&#x27;</span>: <span class="string">&#x27;__main__&#x27;</span>, <span class="string">&#x27;__dict__&#x27;</span>: &lt;attribute <span class="string">&#x27;__dict__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__weakref__&#x27;</span>: &lt;attribute <span class="string">&#x27;__weakref__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__doc__&#x27;</span>: <span class="literal">None</span>&#125;)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">vars</span>(my_obj)</span><br><span class="line">mappingproxy(&#123;<span class="string">&#x27;__module__&#x27;</span>: <span class="string">&#x27;__main__&#x27;</span>, <span class="string">&#x27;__dict__&#x27;</span>: &lt;attribute <span class="string">&#x27;__dict__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__weakref__&#x27;</span>: &lt;attribute <span class="string">&#x27;__weakref__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__doc__&#x27;</span>: <span class="literal">None</span>&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">## 定义属性</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_obj.name = <span class="string">&quot;zhangsan&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 定义属性之后，多了一个&#x27;name&#x27;: &#x27;zhangsan&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_obj.__dict__</span><br><span class="line">mappingproxy(&#123;<span class="string">&#x27;__module__&#x27;</span>: <span class="string">&#x27;__main__&#x27;</span>, <span class="string">&#x27;__dict__&#x27;</span>: &lt;attribute <span class="string">&#x27;__dict__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__weakref__&#x27;</span>: &lt;attribute <span class="string">&#x27;__weakref__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__doc__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;zhangsan&#x27;</span>&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">vars</span>(my_obj)</span><br><span class="line">mappingproxy(&#123;<span class="string">&#x27;__module__&#x27;</span>: <span class="string">&#x27;__main__&#x27;</span>, <span class="string">&#x27;__dict__&#x27;</span>: &lt;attribute <span class="string">&#x27;__dict__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__weakref__&#x27;</span>: &lt;attribute <span class="string">&#x27;__weakref__&#x27;</span> of <span class="string">&#x27;my_obj&#x27;</span> objects&gt;, <span class="string">&#x27;__doc__&#x27;</span>: <span class="literal">None</span>, <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;zhangsan&#x27;</span>&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="类型转换-24个"><a href="#类型转换-24个" class="headerlink" title="类型转换(24个)"></a>类型转换(24个)</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>bool()</code></td>
<td>根据传入的参数的逻辑值创建一个新的布尔值</td>
</tr>
<tr>
<td>2</td>
<td><code>int()</code></td>
<td>根据传入的参数创建一个新的整数</td>
</tr>
<tr>
<td>3</td>
<td><code>float()</code></td>
<td>根据传入的参数创建一个新的浮点数</td>
</tr>
<tr>
<td>4</td>
<td><code>complex()</code></td>
<td>根据传入参数创建一个新的复数</td>
</tr>
<tr>
<td>5</td>
<td><code>str()</code></td>
<td>返回一个对象的字符串表现形式(给用户)</td>
</tr>
<tr>
<td>6</td>
<td><code>bytearray()</code></td>
<td>根据传入的参数创建一个新的字节数组</td>
</tr>
<tr>
<td>7</td>
<td><code>bytes()</code></td>
<td>根据传入的参数创建一个新的不可变字节数组</td>
</tr>
<tr>
<td>8</td>
<td><code>memoryview()</code></td>
<td>根据传入的参数创建一个新的内存查看对象</td>
</tr>
<tr>
<td>9</td>
<td><code>ord()</code></td>
<td>返回Unicode字符对应的整数</td>
</tr>
<tr>
<td>10</td>
<td><code>chr()</code></td>
<td>返回整数所对应的Unicode字符</td>
</tr>
<tr>
<td>11</td>
<td><code>bin()</code></td>
<td>将整数转换成2进制字符串</td>
</tr>
<tr>
<td>12</td>
<td><code>oct()</code></td>
<td>将整数转化成8进制数字符串</td>
</tr>
<tr>
<td>13</td>
<td><code>hex()</code></td>
<td>将整数转换成16进制字符串</td>
</tr>
<tr>
<td>14</td>
<td><code>tuple()</code></td>
<td>根据传入的参数创建一个新的元组</td>
</tr>
<tr>
<td>15</td>
<td><code>dict()</code></td>
<td>根据传入的参数创建一个新的列表</td>
</tr>
<tr>
<td>16</td>
<td><code>list()</code></td>
<td>根据传入的参数创建一个新的字典</td>
</tr>
<tr>
<td>17</td>
<td><code>set()</code></td>
<td>根据传入的参数创建一个新的集合</td>
</tr>
<tr>
<td>18</td>
<td><code>frozenset()</code></td>
<td>根据传入的参数创建一个新的不可变集合</td>
</tr>
<tr>
<td>19</td>
<td><code>enumerate()</code></td>
<td>根据可迭代对象创建枚举对象</td>
</tr>
<tr>
<td>20</td>
<td><code>range()</code></td>
<td>根据传入的参数创建一个新的range对象</td>
</tr>
<tr>
<td>21</td>
<td><code>iter()</code></td>
<td>根据传入的参数创建一个新的可迭代对象</td>
</tr>
<tr>
<td>22</td>
<td><code>slice()</code></td>
<td>根据传入的参数创建一个新的切片对象</td>
</tr>
<tr>
<td>23</td>
<td><code>super()</code></td>
<td>根据传入的参数创建一个新的子类和父类关系的代理对象</td>
</tr>
<tr>
<td>24</td>
<td><code>object()</code></td>
<td>创建一个新的object对象</td>
</tr>
</tbody></table>
<h3 id="bool"><a href="#bool" class="headerlink" title="bool()"></a><code>bool()</code></h3><p>用途：根据传入的参数的逻辑值创建一个新的布尔值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bool</span>()</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bool</span>([])</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bool</span>(<span class="literal">False</span>)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bool</span>(<span class="literal">None</span>)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bool</span>(<span class="number">1</span>)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bool</span>(<span class="literal">True</span>)</span><br><span class="line"><span class="literal">True</span></span><br></pre></td></tr></table></figure>

<h3 id="int"><a href="#int" class="headerlink" title="int()"></a><code>int()</code></h3><p>用途：根据传入的参数创建一个新的整数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">int</span>()         <span class="comment"># 什么都不传时，输出0</span></span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">int</span>(<span class="string">&quot;12&quot;</span>)    <span class="comment"># 将字符串转换成整型</span></span><br><span class="line"><span class="number">12</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">int</span>(<span class="string">&quot;12.0&quot;</span>)     <span class="comment"># 小数类型的字符串不能转换成整型</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">ValueError: invalid literal <span class="keyword">for</span> <span class="built_in">int</span>() <span class="keyword">with</span> base <span class="number">10</span>: <span class="string">&#x27;12.0&#x27;</span>  <span class="comment"># 提示只能是整型类型的字符串才能转换成功</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">int</span>(<span class="number">3.14</span>)</span><br><span class="line"><span class="number">3</span></span><br></pre></td></tr></table></figure>

<h3 id="float"><a href="#float" class="headerlink" title="float()"></a><code>float()</code></h3><p>用途：根据传入的参数创建一个新的浮点数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">float</span>()          <span class="comment"># 什么都不传时，输出0</span></span><br><span class="line"><span class="number">0.0</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">float</span>(<span class="number">3.14</span>)</span><br><span class="line"><span class="number">3.14</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">float</span>(<span class="number">3</span>)</span><br><span class="line"><span class="number">3.0</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">float</span>(<span class="string">&quot;1.2&quot;</span>)   <span class="comment"># 字符串类型的小数</span></span><br><span class="line"><span class="number">1.2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">float</span>(<span class="string">&quot;1&quot;</span>)     <span class="comment"># 字符串类型的整数</span></span><br><span class="line"><span class="number">1.0</span></span><br></pre></td></tr></table></figure>

<h3 id="complex"><a href="#complex" class="headerlink" title="complex()"></a><code>complex()</code></h3><p>用途：根据传入参数创建一个新的复数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">complex</span>() <span class="comment">#当两个参数都不提供时，返回复数0j。</span></span><br><span class="line"><span class="number">0j</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">complex</span>(<span class="string">&#x27;1+2j&#x27;</span>) <span class="comment"># 传入字符串创建复数</span></span><br><span class="line">(<span class="number">1</span>+<span class="number">2j</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">complex</span>(<span class="number">1</span>,<span class="number">2</span>) <span class="comment"># 传入数值创建复数</span></span><br><span class="line">(<span class="number">1</span>+<span class="number">2j</span>)</span><br></pre></td></tr></table></figure>

<h3 id="str"><a href="#str" class="headerlink" title="str()"></a><code>str()</code></h3><p>用途：返回一个对象的字符串表现形式(给用户)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">str</span>()     <span class="comment"># 什么都不传时，输出空，但这也是个字符串类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line"><span class="string">&#x27;&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(a)</span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;str&#x27;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">str</span>(<span class="literal">None</span>)</span><br><span class="line"><span class="string">&#x27;None&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">str</span>(<span class="string">&#x27;abc&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;abc&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">str</span>(<span class="number">123</span>)</span><br><span class="line"><span class="string">&#x27;123&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">str</span>([<span class="number">1</span>, <span class="string">&quot;zhangsan&quot;</span>])   <span class="comment"># 将列表转换成字符串</span></span><br><span class="line"><span class="string">&quot;[1, &#x27;zhangsan&#x27;]&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="string">&quot;[1, &#x27;zhangsan&#x27;]&quot;</span>)  <span class="comment"># 不可逆</span></span><br><span class="line">[<span class="string">&#x27;[&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;,&#x27;</span>, <span class="string">&#x27; &#x27;</span>, <span class="string">&quot;&#x27;&quot;</span>, <span class="string">&#x27;z&#x27;</span>, <span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;n&#x27;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="string">&#x27;s&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;n&#x27;</span>, <span class="string">&quot;&#x27;&quot;</span>, <span class="string">&#x27;]&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">## 要想将字符串类型的列表转换成列表，需要通过下面的内置模块</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> ast <span class="keyword">import</span> literal_eval</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>str_list = <span class="string">&quot;[1, 2, 3, 4]&quot;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>literal_eval(str_list)</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br></pre></td></tr></table></figure>

<h3 id="bytearray"><a href="#bytearray" class="headerlink" title="bytearray()"></a><code>bytearray()</code></h3><p>用途：根据传入的参数创建一个新的字节数组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bytearray</span>(<span class="string">&#x27;abc&#x27;</span>,<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="built_in">bytearray</span>(<span class="string">b&#x27;abc&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bytearray</span>(<span class="string">&#x27;中文&#x27;</span>,<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="built_in">bytearray</span>(<span class="string">b&#x27;\xe4\xb8\xad\xe6\x96\x87&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="bytes"><a href="#bytes" class="headerlink" title="bytes()"></a><code>bytes()</code></h3><p>用途：根据传入的参数创建一个新的不可变字节数组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bytes</span>(<span class="string">&#x27;abc&#x27;</span>,<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="string">b&#x27;abc&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bytes</span>(<span class="string">&#x27;中文&#x27;</span>,<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="string">b&#x27;\xe4\xb8\xad\xe6\x96\x87&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="memoryview"><a href="#memoryview" class="headerlink" title="memoryview()"></a><code>memoryview()</code></h3><p>用途：根据传入的参数创建一个新的内存查看对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>v = <span class="built_in">memoryview</span>(<span class="string">b&#x27;abcefg&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>v[<span class="number">1</span>]</span><br><span class="line"><span class="number">98</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>v[-<span class="number">1</span>]</span><br><span class="line"><span class="number">103</span></span><br></pre></td></tr></table></figure>

<h3 id="ord"><a href="#ord" class="headerlink" title="ord()"></a><code>ord()</code></h3><p>用途：返回Unicode字符对应的整数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">ord</span>(<span class="string">&quot;a&quot;</span>)</span><br><span class="line"><span class="number">97</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">ord</span>(<span class="string">&quot;A&quot;</span>)</span><br><span class="line"><span class="number">65</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">ord</span>(<span class="string">&quot;@&quot;</span>)</span><br><span class="line"><span class="number">64</span></span><br></pre></td></tr></table></figure>

<p><code>SACII</code>对照表</p>
<p><img src="/./pic/watermark,image_d2F0ZXIvYmFpa2UxMTY=,g_7,xp_5,yp_5.jpeg" alt="img"></p>
<h3 id="chr"><a href="#chr" class="headerlink" title="chr()"></a><code>chr()</code></h3><p>用途：返回整数所对应的Unicode字符</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">chr</span>(<span class="number">97</span>)</span><br><span class="line"><span class="string">&#x27;a&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">chr</span>(<span class="number">65</span>)</span><br><span class="line"><span class="string">&#x27;A&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">chr</span>(<span class="number">64</span>)</span><br><span class="line"><span class="string">&#x27;@&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="bin"><a href="#bin" class="headerlink" title="bin()"></a><code>bin()</code></h3><p>用途：将整数转换成2进制字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bin</span>(<span class="number">0</span>)</span><br><span class="line"><span class="string">&#x27;0b0&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">bin</span>(<span class="number">100</span>)</span><br><span class="line"><span class="string">&#x27;0b1100100&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="oct"><a href="#oct" class="headerlink" title="oct()"></a><code>oct()</code></h3><p>用途：将整数转化成8进制数字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">oct</span>(<span class="number">8</span>)</span><br><span class="line"><span class="string">&#x27;0o10&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">oct</span>(<span class="number">10</span>)</span><br><span class="line"><span class="string">&#x27;0o12&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="hex"><a href="#hex" class="headerlink" title="hex()"></a><code>hex()</code></h3><p>用途：将整数转换成16进制字符串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hex</span>(<span class="number">16</span>)</span><br><span class="line"><span class="string">&#x27;0x10&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hex</span>(<span class="number">32</span>)</span><br><span class="line"><span class="string">&#x27;0x20&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="tuple"><a href="#tuple" class="headerlink" title="tuple()"></a><code>tuple()</code></h3><p>用途：根据传入的参数（可迭代对象）创建一个新的元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>T = <span class="built_in">tuple</span>()        <span class="comment"># 什么都不传，创建空元组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>T</span><br><span class="line">()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(T)</span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;tuple&#x27;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">tuple</span>(<span class="string">&quot;1&quot;</span>)        <span class="comment"># 只有一个元素的元组需要在元组元素后面加一个“逗号”</span></span><br><span class="line">(<span class="string">&#x27;1&#x27;</span>,)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">tuple</span>([<span class="string">&quot;a&quot;</span>, <span class="number">12</span>, [<span class="string">&quot;zhangsan&quot;</span>]])         <span class="comment"># 将列表转换成元组</span></span><br><span class="line">(<span class="string">&#x27;a&#x27;</span>, <span class="number">12</span>, [<span class="string">&#x27;zhangsan&#x27;</span>])</span><br></pre></td></tr></table></figure>

<p>值得注意的是，下面这种方式获取的结果也是一个元组。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1</span>,<span class="number">2</span></span><br><span class="line">(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>name = <span class="string">&quot;zhangsan&quot;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>age = <span class="number">18</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>addr = <span class="string">&quot;chengdu&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>name, age, addr</span><br><span class="line">(<span class="string">&#x27;zhangsan&#x27;</span>, <span class="number">18</span>, <span class="string">&#x27;chengdu&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="list"><a href="#list" class="headerlink" title="list()"></a><code>list()</code></h3><p>用途：根据传入的参数（可迭代对象）创建一个新的列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>L = <span class="built_in">list</span>()        <span class="comment"># 什么都不传，创建空列表</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>L</span><br><span class="line">[]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(L)</span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;list&#x27;</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="string">&quot;zhangsan&quot;</span>)        <span class="comment"># 将字符串转换成列表</span></span><br><span class="line">[<span class="string">&#x27;z&#x27;</span>, <span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;n&#x27;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="string">&#x27;s&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;n&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>((<span class="string">&#x27;a&#x27;</span>, <span class="number">12</span>, [<span class="string">&#x27;zhangsan&#x27;</span>]))    <span class="comment"># 将元组转换成列表</span></span><br><span class="line">[<span class="string">&#x27;a&#x27;</span>, <span class="number">12</span>, [<span class="string">&#x27;zhangsan&#x27;</span>]]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="number">1</span>)                            <span class="comment"># 整型是不可迭代对象，因此转换报错</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">TypeError: <span class="string">&#x27;int&#x27;</span> <span class="built_in">object</span> <span class="keyword">is</span> <span class="keyword">not</span> iterable</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="literal">True</span>)                        <span class="comment"># 布尔值是不可迭代对象，因此转换报错</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">TypeError: <span class="string">&#x27;bool&#x27;</span> <span class="built_in">object</span> <span class="keyword">is</span> <span class="keyword">not</span> iterable</span><br></pre></td></tr></table></figure>

<h3 id="dict"><a href="#dict" class="headerlink" title="dict()"></a><code>dict()</code></h3><p>用途：根据传入的参数创建一个新的字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">dict</span>()                 <span class="comment"># 不传入任何参数时，返回空字典</span></span><br><span class="line">&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">dict</span>(name = <span class="string">&quot;zhangsan&quot;</span>, age = <span class="number">18</span>)         <span class="comment"># 传入键值对创建字典</span></span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;zhangsan&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">dict</span>(<span class="built_in">zip</span>([<span class="string">&quot;name&quot;</span>, <span class="string">&quot;age&quot;</span>], [<span class="string">&quot;zhangsan&quot;</span>, <span class="number">18</span>]))  <span class="comment"># 使用zip()函数创建字典，zip函数中两个序列的元素个数以少的为准</span></span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;zhangsan&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">dict</span>(((<span class="string">&quot;name&quot;</span>, <span class="string">&quot;zhangsan&quot;</span>), (<span class="string">&quot;age&quot;</span>, <span class="number">18</span>)))   <span class="comment"># 传入可迭代对象创建字典</span></span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;zhangsan&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br></pre></td></tr></table></figure>

<h3 id="set"><a href="#set" class="headerlink" title="set()"></a><code>set()</code></h3><p>用途：根据传入的参数创建一个新的集合</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt;<span class="built_in">set</span>() <span class="comment"># 不传入参数，创建空集合</span></span><br><span class="line"><span class="built_in">set</span>()</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">set</span>(<span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line">&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>L = [<span class="number">1</span>, <span class="string">&quot;zhangsan&quot;</span>, <span class="string">&quot;abc&quot;</span>, <span class="string">&quot;zhangsan&quot;</span>, <span class="number">1</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">set</span>(L)                  <span class="comment"># 集合的特性--去重</span></span><br><span class="line">&#123;<span class="string">&#x27;abc&#x27;</span>, <span class="number">1</span>, <span class="string">&#x27;zhangsan&#x27;</span>&#125;</span><br></pre></td></tr></table></figure>

<h3 id="frozenset"><a href="#frozenset" class="headerlink" title="frozenset()"></a><code>frozenset()</code></h3><p>用途：根据传入的参数创建一个新的不可变集合</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">frozenset</span>(<span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"><span class="built_in">frozenset</span>(&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="enumerate"><a href="#enumerate" class="headerlink" title="enumerate()"></a><code>enumerate()</code></h3><p>用途：根据可迭代对象创建枚举对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>seasons = [<span class="string">&#x27;Spring&#x27;</span>, <span class="string">&#x27;Summer&#x27;</span>, <span class="string">&#x27;Fall&#x27;</span>, <span class="string">&#x27;Winter&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">enumerate</span>(seasons))</span><br><span class="line">[(<span class="number">0</span>, <span class="string">&#x27;Spring&#x27;</span>), (<span class="number">1</span>, <span class="string">&#x27;Summer&#x27;</span>), (<span class="number">2</span>, <span class="string">&#x27;Fall&#x27;</span>), (<span class="number">3</span>, <span class="string">&#x27;Winter&#x27;</span>)]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">enumerate</span>(seasons, start=<span class="number">1</span>))       <span class="comment">#指定起始值</span></span><br><span class="line">[(<span class="number">1</span>, <span class="string">&#x27;Spring&#x27;</span>), (<span class="number">2</span>, <span class="string">&#x27;Summer&#x27;</span>), (<span class="number">3</span>, <span class="string">&#x27;Fall&#x27;</span>), (<span class="number">4</span>, <span class="string">&#x27;Winter&#x27;</span>)]</span><br></pre></td></tr></table></figure>

<h3 id="range"><a href="#range" class="headerlink" title="range()"></a><code>range()</code></h3><p>用途：根据传入的参数创建一个新的range对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">range</span>(<span class="number">10</span>)          <span class="comment"># 0-9，包括0，不含10</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">10</span>)        <span class="comment"># 1-9，包括1，不含10</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c = <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>)      <span class="comment"># 按照步长3取1-9的数</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a,b,c <span class="comment"># 分别输出a,b,c，结果是一个元组</span></span><br><span class="line">(<span class="built_in">range</span>(<span class="number">0</span>, <span class="number">10</span>), <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">10</span>), <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">10</span>, <span class="number">3</span>))</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(a),<span class="built_in">list</span>(b),<span class="built_in">list</span>(c) <span class="comment"># 分别输出a,b,c的元素，结果是一个元组</span></span><br><span class="line">([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>], [<span class="number">1</span>, <span class="number">4</span>, <span class="number">7</span>])</span><br></pre></td></tr></table></figure>

<h3 id="iter"><a href="#iter" class="headerlink" title="iter()"></a><code>iter()</code></h3><p>用途：根据传入的参数创建一个新的可迭代对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">iter</span>(<span class="string">&#x27;我爱你中国&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;我&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;爱&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;你&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;中&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)</span><br><span class="line"><span class="string">&#x27;国&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a)  <span class="comment"># 取完就报错，要避免报错可通过设置默认值或引入异常处理机制</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">StopIteration</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">## 设置默认值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a, <span class="string">&quot;以获取完成&quot;</span>)</span><br><span class="line"><span class="string">&#x27;以获取完成&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">next</span>(a, <span class="string">&quot;以获取完成&quot;</span>)</span><br><span class="line"><span class="string">&#x27;以获取完成&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">## 引入python异常处理机制</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">iter</span>(<span class="string">&#x27;我爱你中国&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"><span class="meta">... </span>     <span class="keyword">try</span>:</span><br><span class="line"><span class="meta">... </span>         <span class="built_in">print</span>(<span class="built_in">next</span>(a), end=<span class="string">&quot;&quot;</span>)</span><br><span class="line"><span class="meta">... </span>     <span class="keyword">except</span> StopIteration:</span><br><span class="line"><span class="meta">... </span>         <span class="keyword">break</span></span><br><span class="line">...</span><br><span class="line">我爱你中国</span><br></pre></td></tr></table></figure>

<h3 id="slice"><a href="#slice" class="headerlink" title="slice()"></a><code>slice()</code></h3><p>用途：根据传入的参数创建一个新的切片对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>c1 = <span class="built_in">slice</span>(<span class="number">5</span>) <span class="comment"># 定义c1</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c1</span><br><span class="line"><span class="built_in">slice</span>(<span class="literal">None</span>, <span class="number">5</span>, <span class="literal">None</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c2 = <span class="built_in">slice</span>(<span class="number">2</span>,<span class="number">5</span>) <span class="comment"># 定义c2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c2</span><br><span class="line"><span class="built_in">slice</span>(<span class="number">2</span>, <span class="number">5</span>, <span class="literal">None</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c3 = <span class="built_in">slice</span>(<span class="number">1</span>,<span class="number">10</span>,<span class="number">3</span>) <span class="comment"># 定义c3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c3</span><br><span class="line"><span class="built_in">slice</span>(<span class="number">1</span>, <span class="number">10</span>, <span class="number">3</span>)</span><br></pre></td></tr></table></figure>

<h3 id="super"><a href="#super" class="headerlink" title="super()"></a><code>super()</code></h3><p>用途：根据传入的参数创建一个新的子类和父类关系的代理对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#定义父类A</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">A</span>(<span class="title class_ inherited__">object</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;A.__init__&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#定义子类B，继承A</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">B</span>(<span class="title class_ inherited__">A</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;B.__init__&#x27;</span>)</span><br><span class="line">        <span class="built_in">super</span>().__init__()</span><br><span class="line"></span><br><span class="line"><span class="comment">#super调用父类方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = B()</span><br><span class="line">B.__init__</span><br><span class="line">A.__init__</span><br></pre></td></tr></table></figure>

<h3 id="object"><a href="#object" class="headerlink" title="object()"></a><code>object()</code></h3><p>用途：<code>object()</code>函数返回一个空对象，不能向该对象添加新的属性或方法。该对象是所有类的基础，它拥有所有类默认的内置属性和方法。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="built_in">object</span>()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.name = <span class="string">&#x27;kim&#x27;</span> <span class="comment"># 不能设置属性</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;pyshell#9&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">    a.name = <span class="string">&#x27;kim&#x27;</span></span><br><span class="line">AttributeError: <span class="string">&#x27;object&#x27;</span> <span class="built_in">object</span> has no attribute <span class="string">&#x27;name&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="编译执行（4个）"><a href="#编译执行（4个）" class="headerlink" title="编译执行（4个）"></a>编译执行（4个）</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>compile()</code></td>
<td>将字符串编译为代码或者AST对象，使之能够通过exec语句来执行或者eval进行求值</td>
</tr>
<tr>
<td>2</td>
<td><code>eval()</code></td>
<td>执行动态表达式求值</td>
</tr>
<tr>
<td>3</td>
<td><code>exec()</code></td>
<td>执行动态语句块</td>
</tr>
<tr>
<td>4</td>
<td><code>repr()</code></td>
<td>返回一个对象的字符串表现形式(给解释器)</td>
</tr>
</tbody></table>
<h3 id="compile"><a href="#compile" class="headerlink" title="compile()"></a><code>compile()</code></h3><p>用途：将字符串编译为代码或者AST对象，使之能够通过exec语句来执行或者eval进行求值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment">#流程语句使用exec</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>code1 = <span class="string">&#x27;for i in range(0,10): print (i)&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>compile1 = <span class="built_in">compile</span>(code1,<span class="string">&#x27;&#x27;</span>,<span class="string">&#x27;exec&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">exec</span> (compile1)</span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">7</span></span><br><span class="line"><span class="number">8</span></span><br><span class="line"><span class="number">9</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment">#简单求值表达式用eval</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>code2 = <span class="string">&#x27;1 + 2 + 3 + 4&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>compile2 = <span class="built_in">compile</span>(code2,<span class="string">&#x27;&#x27;</span>,<span class="string">&#x27;eval&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">eval</span>(compile2)</span><br><span class="line"><span class="number">10</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="eval"><a href="#eval" class="headerlink" title="eval()"></a><code>eval()</code></h3><p>用途：执行动态表达式求值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">eval</span>(<span class="string">&#x27;1+2+3+4&#x27;</span>)</span><br><span class="line"><span class="number">10</span></span><br></pre></td></tr></table></figure>

<h3 id="exec"><a href="#exec" class="headerlink" title="exec()"></a><code>exec()</code></h3><p>用途：执行动态语句块</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">exec</span>(<span class="string">&#x27;a=1+2&#x27;</span>) <span class="comment">#执行语句</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line"><span class="number">3</span></span><br></pre></td></tr></table></figure>

<h3 id="repr"><a href="#repr" class="headerlink" title="repr()"></a><code>repr()</code></h3><p>用途：返回一个对象的字符串表现形式(给解释器)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="string">&#x27;some text&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">str</span>(a)</span><br><span class="line"><span class="string">&#x27;some text&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">repr</span>(a)</span><br><span class="line"><span class="string">&quot;&#x27;some text&#x27;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="装饰器-3个"><a href="#装饰器-3个" class="headerlink" title="装饰器(3个)"></a>装饰器(3个)</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>property</code></td>
<td>标示属性的装饰器</td>
</tr>
<tr>
<td>2</td>
<td><code>classmethod</code></td>
<td>标示方法为类方法的装饰器</td>
</tr>
<tr>
<td>3</td>
<td><code>staticmethod</code></td>
<td>标示方法为静态方法的装饰器</td>
</tr>
</tbody></table>
<h3 id="property"><a href="#property" class="headerlink" title="property"></a><code>property</code></h3><p>用途：标示属性的装饰器</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">C</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        self._name = <span class="string">&#x27;&#x27;</span></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;i&#x27;m the &#x27;name&#x27; property.&quot;&quot;&quot;</span></span><br><span class="line">        <span class="keyword">return</span> self._name</span><br><span class="line"><span class="meta">    @name.setter</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self,value</span>):</span><br><span class="line">        <span class="keyword">if</span> value <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            <span class="keyword">raise</span> RuntimeError(<span class="string">&#x27;name can not be None&#x27;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            self._name = value</span><br><span class="line"></span><br><span class="line">            </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c = C()</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c.name <span class="comment"># 访问属性</span></span><br><span class="line"><span class="string">&#x27;&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c.name = <span class="literal">None</span> <span class="comment"># 设置属性时进行验证</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;pyshell#84&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">    c.name = <span class="literal">None</span></span><br><span class="line">  File <span class="string">&quot;&lt;pyshell#81&gt;&quot;</span>, line <span class="number">11</span>, <span class="keyword">in</span> name</span><br><span class="line">    <span class="keyword">raise</span> RuntimeError(<span class="string">&#x27;name can not be None&#x27;</span>)</span><br><span class="line">RuntimeError: name can <span class="keyword">not</span> be <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c.name = <span class="string">&#x27;Kim&#x27;</span> <span class="comment"># 设置属性</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c.name <span class="comment"># 访问属性</span></span><br><span class="line"><span class="string">&#x27;Kim&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> c.name <span class="comment"># 删除属性，不提供deleter则不能删除</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;pyshell#87&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">    <span class="keyword">del</span> c.name</span><br><span class="line">AttributeError: can<span class="string">&#x27;t delete attribute</span></span><br><span class="line"><span class="string">&gt;&gt;&gt; c.name</span></span><br><span class="line"><span class="string">&#x27;</span>Kim<span class="string">&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="classmethod"><a href="#classmethod" class="headerlink" title="classmethod"></a><code>classmethod</code></h3><p>用途：标示方法为类方法的装饰器</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">C</span>:</span><br><span class="line"><span class="meta">    @classmethod</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">f</span>(<span class="params">cls,arg1</span>):</span><br><span class="line">        <span class="built_in">print</span>(cls)</span><br><span class="line">        <span class="built_in">print</span>(arg1)</span><br><span class="line"></span><br><span class="line">        </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>C.f(<span class="string">&#x27;类对象调用类方法&#x27;</span>)</span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;__main__.C&#x27;</span>&gt;</span><br><span class="line">类对象调用类方法</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c = C()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c.f(<span class="string">&#x27;类实例对象调用类方法&#x27;</span>)</span><br><span class="line">&lt;<span class="keyword">class</span> <span class="string">&#x27;__main__.C&#x27;</span>&gt;</span><br><span class="line">类实例对象调用类方法</span><br></pre></td></tr></table></figure>

<h3 id="staticmethod"><a href="#staticmethod" class="headerlink" title="staticmethod"></a><code>staticmethod</code></h3><p>用途：标示方法为静态方法的装饰器</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用装饰器定义静态方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">Student</span>(<span class="title class_ inherited__">object</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name</span>):</span><br><span class="line">        self.name = name</span><br><span class="line"><span class="meta">    @staticmethod</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">sayHello</span>(<span class="params">lang</span>):</span><br><span class="line">        <span class="built_in">print</span>(lang)</span><br><span class="line">        <span class="keyword">if</span> lang == <span class="string">&#x27;en&#x27;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&#x27;Welcome!&#x27;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&#x27;你好！&#x27;</span>)</span><br><span class="line"></span><br><span class="line">            </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>Student.sayHello(<span class="string">&#x27;en&#x27;</span>) <span class="comment">#类调用,&#x27;en&#x27;传给了lang参数</span></span><br><span class="line">en</span><br><span class="line">Welcome!</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = Student(<span class="string">&#x27;Kim&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b.sayHello(<span class="string">&#x27;zh&#x27;</span>)  <span class="comment">#类实例对象调用,&#x27;zh&#x27;传给了lang参数</span></span><br><span class="line">zh</span><br><span class="line">你好</span><br></pre></td></tr></table></figure>

<h2 id="反射操作-8个"><a href="#反射操作-8个" class="headerlink" title="反射操作(8个)"></a>反射操作(8个)</h2><table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td><code>__import__()</code></td>
<td>用于动态加载类和函数</td>
</tr>
<tr>
<td>2</td>
<td><code>isinstance()</code></td>
<td>判断对象是否是类或者类型元组中任意类元素的实例</td>
</tr>
<tr>
<td>3</td>
<td><code>issubclass()</code></td>
<td>判断类是否是另外一个类或者类型元组中任意类元素的子类</td>
</tr>
<tr>
<td>4</td>
<td><code>hasattr()</code></td>
<td>检查对象是否含有属性</td>
</tr>
<tr>
<td>5</td>
<td><code>getattr()</code></td>
<td>获取对象的属性值</td>
</tr>
<tr>
<td>6</td>
<td><code>setattr()</code></td>
<td>设置对象的属性值</td>
</tr>
<tr>
<td>7</td>
<td><code>delattr()</code></td>
<td>删除对象的属性</td>
</tr>
<tr>
<td>8</td>
<td><code>callable()</code></td>
<td>检测对象是否可被调用</td>
</tr>
</tbody></table>
<h3 id="import"><a href="#import" class="headerlink" title="__import__()"></a><code>__import__()</code></h3><p>用途：用于动态加载类和函数</p>
<p>a.py 文件代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/env python </span></span><br><span class="line"><span class="comment">#encoding: utf-8</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;在 a.py 文件中 %s&#x27;</span> % <span class="built_in">id</span>(os))</span><br></pre></td></tr></table></figure>

<p>test.py 文件代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/env python</span></span><br><span class="line"><span class="comment">#encoding: utf-8</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="built_in">__import__</span>(<span class="string">&#x27;a&#x27;</span>)        <span class="comment"># 导入 a.py 模块</span></span><br></pre></td></tr></table></figure>

<p>执行 test.py 文件，输出结果为：</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在 a.py 文件中 4394716136</span><br></pre></td></tr></table></figure>

<h3 id="isinstance"><a href="#isinstance" class="headerlink" title="isinstance()"></a><code>isinstance()</code></h3><p>用途：判断对象是否是类或者类型元组中任意类元素的实例</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 判断类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">isinstance</span>(<span class="number">1</span>,<span class="built_in">int</span>)       <span class="comment"># 判断1是不是整型</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">isinstance</span>(<span class="string">&quot;zhangsan&quot;</span>,<span class="built_in">str</span>)   <span class="comment"># 判断是不是字符串</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">isinstance</span>(<span class="number">1</span>,(<span class="built_in">int</span>,<span class="built_in">str</span>))   <span class="comment"># 判断是否后面元组中的一个，如果有一个满足就返回True</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 判断子类和父类</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">A</span>:</span><br><span class="line"><span class="meta">... </span>    <span class="keyword">pass</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">B</span>(<span class="title class_ inherited__">A</span>):</span><br><span class="line"><span class="meta">... </span>    <span class="keyword">pass</span></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">isinstance</span>(A(), A)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(A()) == A</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">isinstance</span>(B(), A)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(B()) == A</span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure>

<p><code>type()</code>与 <code>isinstance()</code>区别，通过下面代码片段可得出以下要点：</p>
<ul>
<li><code>type()</code>不会认为子类是一种父类类型，不考虑继承关系。</li>
<li><code>isinstance() </code>会认为子类是一种父类类型，考虑继承关系。</li>
<li>如果要判断两个类型是否相同推荐使用<code>isinstance()</code>。</li>
</ul>
<h3 id="issubclass"><a href="#issubclass" class="headerlink" title="issubclass()"></a><code>issubclass()</code></h3><p>用途：判断类是否是另外一个类或者类型元组中任意类元素的子类</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">issubclass</span>(<span class="built_in">bool</span>,<span class="built_in">int</span>)   <span class="comment"># 判断布尔值是不是整型的子类，布尔值中False==0，True==1，因此正确</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">0</span> == <span class="literal">False</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1</span> == <span class="literal">True</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1</span> + <span class="literal">True</span>          <span class="comment"># 整型可以直接跟布尔值进行算术运算</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1</span> + <span class="literal">False</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">issubclass</span>(<span class="built_in">bool</span>,<span class="built_in">str</span>)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">issubclass</span>(<span class="built_in">bool</span>,(<span class="built_in">str</span>,<span class="built_in">int</span>))   <span class="comment"># 布尔值是否是字符串或整型的子类</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="hasattr"><a href="#hasattr" class="headerlink" title="hasattr()"></a><code>hasattr()</code></h3><p>用途：检查对象是否含有属性</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#定义类Student</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name</span>):</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line">        </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = Student(<span class="string">&#x27;Aim&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hasattr</span>(s,<span class="string">&#x27;name&#x27;</span>)      <span class="comment"># Student是否含有name属性</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">hasattr</span>(s,<span class="string">&#x27;age&#x27;</span>)       <span class="comment"># Student是否含有age属性</span></span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure>

<h3 id="getattr"><a href="#getattr" class="headerlink" title="getattr()"></a><code>getattr()</code></h3><p>用途：获取对象的属性值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#定义类Student</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name</span>):</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = Student(<span class="string">&#x27;Aim&#x27;</span>)       <span class="comment"># 实例化</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">getattr</span>(s,<span class="string">&#x27;name&#x27;</span>)        <span class="comment"># s中是否存在属性name，如果存在则返回值，不存在报错</span></span><br><span class="line"><span class="string">&#x27;Aim&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">getattr</span>(s,<span class="string">&#x27;age&#x27;</span>, <span class="number">6</span>)      <span class="comment"># s中是否存在属性age，但提供了默认值，返回默认值</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line"> </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">getattr</span>(s,<span class="string">&#x27;age&#x27;</span>)         <span class="comment"># 不存在属性age，未提供默认值，调用报错</span></span><br><span class="line">Traceback (most recent call last):   </span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">AttributeError: <span class="string">&#x27;Student&#x27;</span> <span class="built_in">object</span> has no attribute <span class="string">&#x27;age&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="setattr"><a href="#setattr" class="headerlink" title="setattr()"></a><code>setattr()</code></h3><p>用途：设置对象的属性值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name</span>):</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stu = Student(<span class="string">&#x27;Tom&#x27;</span>)     <span class="comment"># 实例化</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stu.name                 <span class="comment"># stu的name属性现在的值是&#x27;Tom&#x27;</span></span><br><span class="line"><span class="string">&#x27;Tom&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">setattr</span>(stu, <span class="string">&quot;name&quot;</span>, <span class="string">&quot;Zhangsan&quot;</span>)  <span class="comment"># 修改属性值为&quot;Zhangsan&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stu.name</span><br><span class="line"><span class="string">&#x27;Zhangsan&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="delattr"><a href="#delattr" class="headerlink" title="delattr()"></a><code>delattr()</code></h3><p>用途：删除对象的属性</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#定义类A</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">A</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,name</span>):</span><br><span class="line">        self.name = name</span><br><span class="line">        </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">sayHello</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;hello&#x27;</span>,self.name)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = A(<span class="string">&quot;Tony&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">## 测试属性和方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.name</span><br><span class="line"><span class="string">&#x27;Tony&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.sayHello()</span><br><span class="line">hello Tony</span><br><span class="line"></span><br><span class="line"><span class="comment">## 删除属性</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">delattr</span>(a, <span class="string">&quot;name&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">## 测试属性和方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.name</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">AttributeError: <span class="string">&#x27;A&#x27;</span> <span class="built_in">object</span> has no attribute <span class="string">&#x27;name&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a.sayHello()</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">6</span>, <span class="keyword">in</span> sayHello</span><br><span class="line">AttributeError: <span class="string">&#x27;A&#x27;</span> <span class="built_in">object</span> has no attribute <span class="string">&#x27;name&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="callable"><a href="#callable" class="headerlink" title="callable()"></a><code>callable()</code></h3><p>用途：检测对象是否可被调用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">class</span> <span class="title class_">B</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__call__</span>(<span class="params">self</span>):      <span class="comment"># 有这个方法才能被调用</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;instances are callable now.&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">## 检测类B是否是可调用对象</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">callable</span>(B)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用类B，实例化</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = B()</span><br><span class="line"></span><br><span class="line"><span class="comment">## 检查实例b是否是可调用对象</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">callable</span>(b)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 调用实例B</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b()</span><br><span class="line">instances are <span class="built_in">callable</span> now.</span><br></pre></td></tr></table></figure>

<p>注意，在这个例子中，<code>__call__</code> 函数的定义其实就是让执行 <code>b()</code> 的时候所调用的函数，这个非常关键。</p>
<h2 id="其它lambda"><a href="#其它lambda" class="headerlink" title="其它lambda()"></a>其它<code>lambda()</code></h2><p><code>lambda表达式</code>可以用来声明匿名函数。lambda 函数是一种简单的、在同一行中定义函数的方法。<code>lambda()</code>函数实际生成了一个函数对象。</p>
<p><code>lambda 表达式</code>只允许包含一个表达式，不能包含复杂语句，该表达式的计算结果就是函数的返回值。</p>
<ul>
<li><p>lambda只是一个表达式，函数体比def简单很多。</p>
</li>
<li><p>lambda的主体是一个表达式，而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。</p>
</li>
<li><p>lambda表达式是起到一个函数速写的作用。允许在代码内嵌入一个函数的定义。</p>
</li>
</ul>
<p><code>lambda</code>表达式的语法格式如下：<code>name = lambda 参数: 表达式</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 计算两个数的加法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>Sum = <span class="keyword">lambda</span> x, y: x + y</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>Sum(<span class="number">2</span>, <span class="number">4</span>)</span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>Sum(<span class="number">3</span>, <span class="number">9</span>)</span><br><span class="line"><span class="number">12</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 乘方</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="keyword">lambda</span> x: x ** <span class="number">2</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a(<span class="number">10</span>)</span><br><span class="line"><span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a(<span class="number">100</span>)</span><br><span class="line"><span class="number">10000</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io">rushiy</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io/python/python%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/">https://rushiy.gitee.io/python/python%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://rushiy.gitee.io" target="_blank">如是的学习博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/python/">python</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">rushiy</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:rushiy@reg.email.cn" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E5%AD%97%E8%BF%90%E7%AE%97%E7%B1%BB%EF%BC%887%E4%B8%AA%EF%BC%89"><span class="toc-number">1.</span> <span class="toc-text">数字运算类（7个）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#abs"><span class="toc-number">1.1.</span> <span class="toc-text">abs()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#divmod-a-b"><span class="toc-number">1.2.</span> <span class="toc-text">divmod(a, b)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#max"><span class="toc-number">1.3.</span> <span class="toc-text">max()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#min"><span class="toc-number">1.4.</span> <span class="toc-text">min()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#pow-x-y-z"><span class="toc-number">1.5.</span> <span class="toc-text">pow(x, y[, z])</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#round-x-n"><span class="toc-number">1.6.</span> <span class="toc-text">round(x[, n])</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#sum-iterable-start"><span class="toc-number">1.7.</span> <span class="toc-text">sum(iterable[, start]) </span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%A4%E4%BA%92%E6%93%8D%E4%BD%9C-2%E4%B8%AA"><span class="toc-number">2.</span> <span class="toc-text">交互操作(2个)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#print"><span class="toc-number">2.1.</span> <span class="toc-text">print()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#input"><span class="toc-number">2.2.</span> <span class="toc-text">input()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E6%93%8D%E4%BD%9C-2%E4%B8%AA"><span class="toc-number">3.</span> <span class="toc-text">变量操作(2个)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#globals"><span class="toc-number">3.1.</span> <span class="toc-text">globals()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#locals"><span class="toc-number">3.2.</span> <span class="toc-text">locals</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%8F%E5%88%97%E6%93%8D%E4%BD%9C-8%E4%B8%AA"><span class="toc-number">4.</span> <span class="toc-text">序列操作(8个)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#all"><span class="toc-number">4.1.</span> <span class="toc-text">all()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#any"><span class="toc-number">4.2.</span> <span class="toc-text">any()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#filter-function-iterable"><span class="toc-number">4.3.</span> <span class="toc-text">filter(function, iterable)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map"><span class="toc-number">4.4.</span> <span class="toc-text">map()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#next"><span class="toc-number">4.5.</span> <span class="toc-text">next()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#reversed"><span class="toc-number">4.6.</span> <span class="toc-text">reversed()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#sorted"><span class="toc-number">4.7.</span> <span class="toc-text">sorted()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#zip"><span class="toc-number">4.8.</span> <span class="toc-text">zip()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C%EF%BC%881%E4%B8%AA%EF%BC%89"><span class="toc-number">5.</span> <span class="toc-text">文件操作（1个）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E6%93%8D%E4%BD%9C-9%E4%B8%AA"><span class="toc-number">6.</span> <span class="toc-text">对象操作(9个)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#help"><span class="toc-number">6.1.</span> <span class="toc-text">help()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dir"><span class="toc-number">6.2.</span> <span class="toc-text">dir()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#id"><span class="toc-number">6.3.</span> <span class="toc-text">id()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#hash"><span class="toc-number">6.4.</span> <span class="toc-text">hash()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#type"><span class="toc-number">6.5.</span> <span class="toc-text">type()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#len"><span class="toc-number">6.6.</span> <span class="toc-text">len()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ascii"><span class="toc-number">6.7.</span> <span class="toc-text">ascii()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#format"><span class="toc-number">6.8.</span> <span class="toc-text">format()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#vars"><span class="toc-number">6.9.</span> <span class="toc-text">vars()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2-24%E4%B8%AA"><span class="toc-number">7.</span> <span class="toc-text">类型转换(24个)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#bool"><span class="toc-number">7.1.</span> <span class="toc-text">bool()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#int"><span class="toc-number">7.2.</span> <span class="toc-text">int()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#float"><span class="toc-number">7.3.</span> <span class="toc-text">float()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#complex"><span class="toc-number">7.4.</span> <span class="toc-text">complex()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#str"><span class="toc-number">7.5.</span> <span class="toc-text">str()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bytearray"><span class="toc-number">7.6.</span> <span class="toc-text">bytearray()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bytes"><span class="toc-number">7.7.</span> <span class="toc-text">bytes()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#memoryview"><span class="toc-number">7.8.</span> <span class="toc-text">memoryview()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ord"><span class="toc-number">7.9.</span> <span class="toc-text">ord()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#chr"><span class="toc-number">7.10.</span> <span class="toc-text">chr()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bin"><span class="toc-number">7.11.</span> <span class="toc-text">bin()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#oct"><span class="toc-number">7.12.</span> <span class="toc-text">oct()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#hex"><span class="toc-number">7.13.</span> <span class="toc-text">hex()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#tuple"><span class="toc-number">7.14.</span> <span class="toc-text">tuple()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#list"><span class="toc-number">7.15.</span> <span class="toc-text">list()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dict"><span class="toc-number">7.16.</span> <span class="toc-text">dict()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#set"><span class="toc-number">7.17.</span> <span class="toc-text">set()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#frozenset"><span class="toc-number">7.18.</span> <span class="toc-text">frozenset()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#enumerate"><span class="toc-number">7.19.</span> <span class="toc-text">enumerate()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#range"><span class="toc-number">7.20.</span> <span class="toc-text">range()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#iter"><span class="toc-number">7.21.</span> <span class="toc-text">iter()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#slice"><span class="toc-number">7.22.</span> <span class="toc-text">slice()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#super"><span class="toc-number">7.23.</span> <span class="toc-text">super()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#object"><span class="toc-number">7.24.</span> <span class="toc-text">object()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E6%89%A7%E8%A1%8C%EF%BC%884%E4%B8%AA%EF%BC%89"><span class="toc-number">8.</span> <span class="toc-text">编译执行（4个）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#compile"><span class="toc-number">8.1.</span> <span class="toc-text">compile()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#eval"><span class="toc-number">8.2.</span> <span class="toc-text">eval()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#exec"><span class="toc-number">8.3.</span> <span class="toc-text">exec()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#repr"><span class="toc-number">8.4.</span> <span class="toc-text">repr()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A3%85%E9%A5%B0%E5%99%A8-3%E4%B8%AA"><span class="toc-number">9.</span> <span class="toc-text">装饰器(3个)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#property"><span class="toc-number">9.1.</span> <span class="toc-text">property</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#classmethod"><span class="toc-number">9.2.</span> <span class="toc-text">classmethod</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#staticmethod"><span class="toc-number">9.3.</span> <span class="toc-text">staticmethod</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%8D%E5%B0%84%E6%93%8D%E4%BD%9C-8%E4%B8%AA"><span class="toc-number">10.</span> <span class="toc-text">反射操作(8个)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#import"><span class="toc-number">10.1.</span> <span class="toc-text">__import__()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#isinstance"><span class="toc-number">10.2.</span> <span class="toc-text">isinstance()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#issubclass"><span class="toc-number">10.3.</span> <span class="toc-text">issubclass()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#hasattr"><span class="toc-number">10.4.</span> <span class="toc-text">hasattr()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#getattr"><span class="toc-number">10.5.</span> <span class="toc-text">getattr()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#setattr"><span class="toc-number">10.6.</span> <span class="toc-text">setattr()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#delattr"><span class="toc-number">10.7.</span> <span class="toc-text">delattr()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#callable"><span class="toc-number">10.8.</span> <span class="toc-text">callable()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B6%E5%AE%83lambda"><span class="toc-number">11.</span> <span class="toc-text">其它lambda()</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django模板系统"/></a><div class="content"><a class="title" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统">Django模板系统</a><time datetime="2022-11-04T07:08:03.000Z" title="发表于 2022-11-04 15:08:03">2022-11-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django视图"/></a><div class="content"><a class="title" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图">Django视图</a><time datetime="2022-11-03T07:07:51.000Z" title="发表于 2022-11-03 15:07:51">2022-11-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django路由系统"/></a><div class="content"><a class="title" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统">Django路由系统</a><time datetime="2022-11-02T07:07:34.000Z" title="发表于 2022-11-02 15:07:34">2022-11-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="走进Django"/></a><div class="content"><a class="title" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django">走进Django</a><time datetime="2022-11-01T07:01:20.000Z" title="发表于 2022-11-01 15:01:20">2022-11-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="python常用模块"/></a><div class="content"><a class="title" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块">python常用模块</a><time datetime="2022-10-28T15:34:07.000Z" title="发表于 2022-10-28 23:34:07">2022-10-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By rushiy</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>